#!/usr/bin/env python
# encoding: utf-8
"""
test_portlet.py

Created by Manabu Terada on 2009-09-25.
Copyright (c) 2009 CMScom. All rights reserved.
"""
from zope.component import getUtility, getMultiAdapter

from Products.GenericSetup.utils import _getDottedName

from plone.portlets.interfaces import IPortletType
from plone.portlets.interfaces import IPortletManager
from plone.portlets.interfaces import IPortletAssignment
from plone.portlets.interfaces import IPortletDataProvider
from plone.portlets.interfaces import IPortletRenderer

from plone.app.portlets.storage import PortletAssignmentMapping
from Products.CMFPlone.tests import dummy

import c2.site.icos09
import base

from c2.site.icos09.browser.portlets import banner, login, column, \
                    blog, news_column, news, event

class TestAddPortlet(base.IcosTestCase):
    """  """
    def afterSetUp(self):
        self.setRoles(('Manager',))
        qi = self.portal.portal_quickinstaller
        qi.installProduct('c2.site.icos09')
        # qi.installProduct('webcouturier.dropdownmenu')
        
        
    def testPortletTypeRegistered(self):
        portlet = getUtility(IPortletType, name='icos09.banner_portlet')
        self.assertEquals(portlet.addview, 'icos09.banner_portlet')
        portlet2 = getUtility(IPortletType, name='icos09.login_portlet')
        self.assertEquals(portlet2.addview, 'icos09.login_portlet')
        portlet3 = getUtility(IPortletType, name='icos09.column_portlet')
        self.assertEquals(portlet3.addview, 'icos09.column_portlet')
        portlet4 = getUtility(IPortletType, name='icos09.blog_portlet')
        self.assertEquals(portlet4.addview, 'icos09.blog_portlet')
        portlet5 = getUtility(IPortletType, name='icos09.news_column_portlet')
        self.assertEquals(portlet5.addview, 'icos09.news_column_portlet')
        portlet6 = getUtility(IPortletType, name='icos09.news_portlet')
        self.assertEquals(portlet6.addview, 'icos09.news_portlet')
        portlet7 = getUtility(IPortletType, name='icos09.event_portlet')
        self.assertEquals(portlet7.addview, 'icos09.event_portlet')

    def testRegisteredInterfaces(self):
        portlet = getUtility(IPortletType, name='icos09.banner_portlet')
        registered_interfaces = [_getDottedName(i) for i in portlet.for_] 
        registered_interfaces.sort() 
        self.assertEquals(['zope.interface.Interface'],
                    registered_interfaces)
    
        portlet2 = getUtility(IPortletType, name='icos09.login_portlet')
        registered_interfaces = [_getDottedName(i) for i in portlet2.for_] 
        registered_interfaces.sort() 
        self.assertEquals(['zope.interface.Interface'],
                    registered_interfaces)
    
        portlet3 = getUtility(IPortletType, name='icos09.column_portlet')
        registered_interfaces = [_getDottedName(i) for i in portlet3.for_] 
        registered_interfaces.sort() 
        self.assertEquals(['zope.interface.Interface'],
                    registered_interfaces)
        
        #
        portlet4 = getUtility(IPortletType, name='icos09.news_portlet')
        registered_interfaces = [_getDottedName(i) for i in portlet4.for_] 
        registered_interfaces.sort() 
        self.assertEquals(['zope.interface.Interface'],
                    registered_interfaces)
        #
        portlet5 = getUtility(IPortletType, name='icos09.news_column_portlet')
        registered_interfaces = [_getDottedName(i) for i in portlet5.for_] 
        registered_interfaces.sort() 
        self.assertEquals(['zope.interface.Interface'],
                    registered_interfaces)
        #
        portlet6 = getUtility(IPortletType, name='icos09.blog_portlet')
        registered_interfaces = [_getDottedName(i) for i in portlet6.for_] 
        registered_interfaces.sort() 
        self.assertEquals(['zope.interface.Interface'],
                    registered_interfaces)
        #
        portlet7 = getUtility(IPortletType, name='icos09.event_portlet')
        registered_interfaces = [_getDottedName(i) for i in portlet7.for_] 
        registered_interfaces.sort() 
        self.assertEquals(['zope.interface.Interface'],
                    registered_interfaces)
    
    def testInterfaces(self):
        portlet = banner.Assignment()
        self.failUnless(IPortletAssignment.providedBy(portlet))
        self.failUnless(IPortletDataProvider.providedBy(portlet.data))
    
        portlet2 = login.Assignment()
        self.failUnless(IPortletAssignment.providedBy(portlet2))
        self.failUnless(IPortletDataProvider.providedBy(portlet2.data))
    
        portlet3 = column.Assignment()
        self.failUnless(IPortletAssignment.providedBy(portlet3))
        self.failUnless(IPortletDataProvider.providedBy(portlet3.data))
    
        portlet4 = blog.Assignment()
        self.failUnless(IPortletAssignment.providedBy(portlet4))
        self.failUnless(IPortletDataProvider.providedBy(portlet4.data))
        
        portlet5 = news_column.Assignment()
        self.failUnless(IPortletAssignment.providedBy(portlet5))
        self.failUnless(IPortletDataProvider.providedBy(portlet5.data))
        
        portlet6 = news.Assignment()
        self.failUnless(IPortletAssignment.providedBy(portlet6))
        self.failUnless(IPortletDataProvider.providedBy(portlet6.data))
        
        portlet7 = event.Assignment()
        self.failUnless(IPortletAssignment.providedBy(portlet7))
        self.failUnless(IPortletDataProvider.providedBy(portlet7.data))
    
    def testInvokeAddview(self):
        portlet = getUtility(IPortletType, name='icos09.banner_portlet')
        mapping = self.portal.restrictedTraverse('++contextportlets++plone.leftcolumn')
        for m in mapping.keys():
            del mapping[m]
        addview = mapping.restrictedTraverse('+/' + portlet.addview)    
        addview.createAndAdd(data={})
        self.assertEquals(len(mapping), 1)
        self.failUnless(isinstance(mapping.values()[0], banner.Assignment))

        portlet2 = getUtility(IPortletType, name='icos09.login_portlet')
        mapping = self.portal.restrictedTraverse('++contextportlets++plone.leftcolumn')
        for m in mapping.keys():
            del mapping[m]
        addview2 = mapping.restrictedTraverse('+/' + portlet2.addview)    
        addview2.createAndAdd(data={})
        self.assertEquals(len(mapping), 1)
        self.failUnless(isinstance(mapping.values()[0], login.Assignment))

        portlet3 = getUtility(IPortletType, name='icos09.column_portlet')
        mapping = self.portal.restrictedTraverse('++contextportlets++plone.leftcolumn')
        for m in mapping.keys():
            del mapping[m]
        addview3 = mapping.restrictedTraverse('+/' + portlet3.addview)    
        addview3.createAndAdd(data={})
        self.assertEquals(len(mapping), 1)
        self.failUnless(isinstance(mapping.values()[0], column.Assignment))
        
        portlet4 = getUtility(IPortletType, name='icos09.news_column_portlet')
        mapping = self.portal.restrictedTraverse('++contextportlets++plone.leftcolumn')
        for m in mapping.keys():
            del mapping[m]
        addview4 = mapping.restrictedTraverse('+/' + portlet4.addview)    
        addview4.createAndAdd(data={})
        self.assertEquals(len(mapping), 1)
        self.failUnless(isinstance(mapping.values()[0], news_column.Assignment))
        
        portlet5 = getUtility(IPortletType, name='icos09.news_portlet')
        mapping = self.portal.restrictedTraverse('++contextportlets++plone.leftcolumn')
        for m in mapping.keys():
            del mapping[m]
        addview5 = mapping.restrictedTraverse('+/' + portlet5.addview)    
        addview5.createAndAdd(data={})
        self.assertEquals(len(mapping), 1)
        self.failUnless(isinstance(mapping.values()[0], news.Assignment))
        
        portlet6 = getUtility(IPortletType, name='icos09.blog_portlet')
        mapping = self.portal.restrictedTraverse('++contextportlets++plone.leftcolumn')
        for m in mapping.keys():
            del mapping[m]
        addview6 = mapping.restrictedTraverse('+/' + portlet6.addview)    
        addview6.createAndAdd(data={})
        self.assertEquals(len(mapping), 1)
        self.failUnless(isinstance(mapping.values()[0], blog.Assignment))
        
        portlet7 = getUtility(IPortletType, name='icos09.event_portlet')
        mapping = self.portal.restrictedTraverse('++contextportlets++plone.leftcolumn')
        for m in mapping.keys():
            del mapping[m]
        addview7 = mapping.restrictedTraverse('+/' + portlet7.addview)    
        addview7.createAndAdd(data={})
        self.assertEquals(len(mapping), 1)
        self.failUnless(isinstance(mapping.values()[0], event.Assignment))
    
    def testInvokeEditView(self):
        mapping = PortletAssignmentMapping()
        request = self.folder.REQUEST
    
        mapping['foo'] = banner.Assignment()
        editview = getMultiAdapter((mapping['foo'], request), name='edit')
        self.failUnless(isinstance(editview, banner.EditForm))

        mapping['foo2'] = login.Assignment()
        editview2 = getMultiAdapter((mapping['foo2'], request), name='edit')
        self.failUnless(isinstance(editview2, login.EditForm))

        mapping['foo3'] = column.Assignment()
        editview3 = getMultiAdapter((mapping['foo3'], request), name='edit')
        self.failUnless(isinstance(editview3, column.EditForm))

        mapping['foo4'] = blog.Assignment()
        editview4 = getMultiAdapter((mapping['foo4'], request), name='edit')
        self.failUnless(isinstance(editview4, blog.EditForm))
        
        mapping['foo5'] = news_column.Assignment()
        editview5 = getMultiAdapter((mapping['foo5'], request), name='edit')
        self.failUnless(isinstance(editview5, news_column.EditForm))
        
        mapping['foo6'] = news.Assignment()
        editview6 = getMultiAdapter((mapping['foo6'], request), name='edit')
        self.failUnless(isinstance(editview6, news.EditForm))
        
        mapping['foo7'] = event.Assignment()
        editview7 = getMultiAdapter((mapping['foo7'], request), name='edit')
        self.failUnless(isinstance(editview7, event.EditForm))
    
    def testRenderer(self):
        context = self.folder
        request = self.folder.REQUEST
        view = self.folder.restrictedTraverse('@@plone')
        manager = getUtility(IPortletManager, name='plone.leftcolumn', context=self.portal)
        assignment = banner.Assignment()    
        renderer = getMultiAdapter((context, request, view, manager, assignment), IPortletRenderer)
        self.failUnless(isinstance(renderer, banner.Renderer))

        assignment2 = login.Assignment()    
        renderer2 = getMultiAdapter((context, request, view, manager, assignment2), IPortletRenderer)
        self.failUnless(isinstance(renderer2, login.Renderer))

        assignment3 = column.Assignment()    
        renderer3 = getMultiAdapter((context, request, view, manager, assignment3), IPortletRenderer)
        self.failUnless(isinstance(renderer3, column.Renderer))

        assignment4 = blog.Assignment()    
        renderer4 = getMultiAdapter((context, request, view, manager, assignment4), IPortletRenderer)
        self.failUnless(isinstance(renderer4, blog.Renderer))
        
        assignment5 = news_column.Assignment()    
        renderer5 = getMultiAdapter((context, request, view, manager, assignment5), IPortletRenderer)
        self.failUnless(isinstance(renderer5, news_column.Renderer))
        
        assignment6 = news.Assignment()    
        renderer6 = getMultiAdapter((context, request, view, manager, assignment6), IPortletRenderer)
        self.failUnless(isinstance(renderer6, news.Renderer))
        
        assignment7 = event.Assignment()    
        renderer7 = getMultiAdapter((context, request, view, manager, assignment7), IPortletRenderer)
        self.failUnless(isinstance(renderer7, event.Renderer))

def test_suite():
    from unittest import TestSuite, makeSuite
    suite = TestSuite()
    suite.addTest(makeSuite(TestAddPortlet))
    return suite

